package com.sino.androiddesign.recyclerviewdemo


import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.*
import android.view.animation.DecelerateInterpolator
import android.widget.*

import com.sino.androiddesign.R

/**
 * 添加头部动画 和 item 动画
 * todo 需要添加头部动画
 *
 */
class RvAnimationFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_rv_animation, container, false)
    }

}

/**
 * 头部
 */
class RecyclerHeader : LinearLayout {

    //动画执行时间
    private final var ROTATE_DURATION: Int = 180

    companion object {
        public val STATE_NORMAL = 0
        public val STATE_READY = 1
        public val STATE_REFRESHING = 2
    }

    // 当前的状态
    private var mState: Int = STATE_NORMAL
    //头布局
    private var mContainer: LinearLayout? = null
    private var mArrowImageView: ImageView? = null
    private var mHintTextView: TextView? = null

    private var mTitleTextView: TextView? = null
    private var mRelativeLayout: RelativeLayout? = null

    constructor(context: Context?) : this(context, null);
    constructor(context: Context?, attributeSet: AttributeSet?) : this(context, attributeSet, 0);
    constructor(context: Context?, attributeSet: AttributeSet?, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context?) {
        var layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0)
        mContainer = LayoutInflater.from(context).inflate(R.layout.rv_header_layout, this, false) as LinearLayout

        addView(mContainer, layoutParams)
        gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
        //初始化控件
        mRelativeLayout = mContainer!!.findViewById(R.id.pullRefresh_reality_content)
        mArrowImageView = mContainer!!.findViewById(R.id.pullRefresh_arrow)
        mHintTextView = mContainer!!.findViewById(R.id.text)
        mTitleTextView = mContainer!!.findViewById(R.id.pullRefresh_title)

    }

    fun setState(state: Int) {
        if (mState == state) return

        when (state) {
            STATE_NORMAL -> {
                mHintTextView!!.text = "下拉刷新"
            }
            STATE_READY -> {
                if (mState != STATE_READY) {

                    mHintTextView!!.text = "松开刷新数据"
                }
            }
            STATE_REFRESHING -> {
                mHintTextView!!.text = "正在加载..."

            }
        }
        mState = state
    }

    fun setPullImage(img: Int) {
        mArrowImageView!!.setImageResource(R.mipmap.ic_launcher)
    }

    fun setPullContent(text: String) {
        mTitleTextView!!.text = text
    }

    fun getRealityHeight(): Int {
        return mRelativeLayout!!.height
    }

    fun setVisibleHeight(height: Int) {
        var mHeight: Int = if (height < 0) {
            0
        } else {
            height
        }
        var layoutParams = mContainer!!.layoutParams
        layoutParams.height = mHeight
        mContainer!!.layoutParams = layoutParams
    }

    fun getVisibleHeight(): Int {
        return mContainer!!.layoutParams.height
    }

}

/**
 * 尾部
 */
class RecyclerViewFooter : LinearLayout {


    companion object {
        val STATE_NORMAL = 0
        val STATE_READY = 1
        val STATE_LOADING = 2
    }

    private var mContentView: RelativeLayout? = null
    private var mProgressBar: ProgressBar? = null
    private var mHintView: TextView? = null

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attributeSet: AttributeSet?) : this(context, null, 0)
    constructor(context: Context?, attributeSet: AttributeSet?, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
        initView()
    }

    private fun initView() {
        var moreView = LayoutInflater.from(context).inflate(R.layout.pullrefrefh_recyclerview_footer, null) as LinearLayout
        addView(moreView)
        moreView!!.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        mContentView = moreView.findViewById<RelativeLayout>(R.id.pullrefrefh_footer_content)
        mProgressBar = moreView.findViewById(R.id.pullrefrefh_footer_ProgressBar)
        mHintView = moreView.findViewById(R.id.pullrefrefh_footer_hint_TextView)
    }

    fun setState(state: Int) {
        mHintView!!.visibility = View.INVISIBLE
        mProgressBar!!.visibility = View.INVISIBLE
        when (state) {
            STATE_READY -> {
                mHintView!!.visibility = View.VISIBLE
                mHintView!!.text = "松开载入更多"
            }
            STATE_LOADING -> {
                mProgressBar!!.visibility = View.VISIBLE
            }
            STATE_NORMAL -> {
                mHintView!!.visibility = View.VISIBLE
                mHintView!!.text = "查看更多"
            }
        }
    }

    fun setBottomMargin(height: Int) {
        if (height < 0) return
        var layoutParams = mContentView!!.layoutParams as LayoutParams
        layoutParams.bottomMargin = height
        mContentView!!.layoutParams = layoutParams
    }


    fun getBottomMargin(): Int {
        var layoutParams = mContentView!!.layoutParams as LayoutParams
        return layoutParams.bottomMargin
    }

    fun hide() {
        var layoutParams = mContentView!!.layoutParams as LayoutParams
        layoutParams.height = 0
        mContentView!!.layoutParams = layoutParams
    }

    fun show() {
        var layoutParams = mContentView!!.layoutParams as LayoutParams
        layoutParams.height = LayoutParams.WRAP_CONTENT
        mContentView!!.layoutParams = layoutParams
    }
}

class MessageRelativeLayout : RelativeLayout{

    private lateinit var mHeaderMessageView: LinearLayout
private lateinit var mHeaderMessageText:TextView

    private var mHeaderMessageViewHeight:Int = 0

    private lateinit var mScroller: Scroller

    constructor(context: Context?):this(context,null)
    constructor(context: Context?,attributeSet: AttributeSet?):this(context,attributeSet,0)
    constructor(context: Context? ,attributeSet: AttributeSet?,defStyleAttr: Int):super(context,attributeSet,defStyleAttr){
        initView()
    }

    private fun initView() {
            mScroller = Scroller(context,DecelerateInterpolator())
        mHeaderMessageView = LayoutInflater.from(context).inflate(R.layout.pullrefresh_header_message,this,false) as LinearLayout
      mHeaderMessageView .layoutParams= LayoutParams(LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        mHeaderMessageText = mHeaderMessageView.findViewById(R.id.pullRefresh_message)

        mHeaderMessageText.viewTreeObserver.addOnGlobalLayoutListener {
            mHeaderMessageViewHeight = mHeaderMessageText.height
            viewTreeObserver.removeOnGlobalLayoutListener { this }
        }

    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        addView(mHeaderMessageView,1)
    }
    fun showMessage(){
        mScroller.startScroll(0,getHeaderMessageViewHeight(),0,0,200)
        invalidate()
    }

    fun hideMessage(){
        mScroller.startScroll(0,getVisibleHeight(),0,-getVisibleHeight(),200)
        invalidate()
    }

    fun  setMessage(message:String){
        mHeaderMessageText.text = message
    }

    fun setVisibleHeight(height:Int){
        var mHeight :Int= if (height < 0){
            0
        }else{
            height
        }
        var layoutParams = mHeaderMessageView.layoutParams
        layoutParams.height = mHeight
        mHeaderMessageView.layoutParams = layoutParams
    }


    fun  getHeaderMessageViewHeight():Int{
        return mHeaderMessageViewHeight
    }

    fun getVisibleHeight():Int{
        return mHeaderMessageView.layoutParams.height
    }

    override fun computeScroll() {
        if(mScroller.computeScrollOffset()){
            setVisibleHeight(mScroller.currY)
            postInvalidate()
        }
        super.computeScroll()
    }
}




